//
//  WelcomeViewController.swift
//  微博
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        loadUI()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //添加动画
        loadAnimation()
    }
    
    //将要加载视图
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //添加自动布局
//        loadAnimation()
    }
    //设置动画
    private func loadAnimation() {
        textLabel.alpha = 0
        //设置一个偏移量
        let offset = -UIScreen.mainScreen().bounds.size.height + 200
        
        //设置自动布局动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //改变自动布局的偏移量
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view.snp_bottom).offset(offset)
            })
            //强制刷新自动布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
               UIView.animateWithDuration(1.5, animations: { () -> Void in
                self.textLabel.alpha = 1
                }, completion: { (_) -> Void in
                    //回调的方法
                    NSNotificationCenter.defaultCenter().postNotificationName(rootVC, object: nil)
               })
                
        }
        
    }
    
    
    //添加自动布局
    private func loadUI() {
        //添加控件
        view.addSubview(iconView)
        view.addSubview(textLabel)
        //设置自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //加载网络图片
        iconView.sd_setImageWithURL(UserAccontViewMedol().iconURL)
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
    }
    
    override func loadView() {
        view = backgroundView
    }
 
    
    //懒加载
    private lazy var backgroundView:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var textLabel:UILabel = {
        let label = UILabel()
        label.text = (UserAccontViewMedol().userName ?? "") + " 欢迎回来"
        label.font = UIFont.systemFontOfSize(16)
        label.contentMode = .Center
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    
}
